#ifndef __HTTPSERVER_HPP__
#define __HTTPSERVER_HPP__
#include <iostream>
#include "TcpServer.hpp"
#include "Protocol.hpp"
#include <pthread.h>

struct ThreadData
{
    ThreadData(socket_ns::socket_ptr sock = nullptr) :  _sock(sock){}
    socket_ns::socket_ptr _sock;
};

class HttpServer
{
    public:
        HttpServer(int port):_running(false)
        {
            signal(SIGPIPE,SIG_IGN);
            _tcpsvr = TcpServer::getInstance(port,Entrance::HandlerRequest);
        }
        void Loop()
        {
            _running = true;
            while(_running)
            {
                InetAddr addr;
                socket_ns::socket_ptr* sp = new socket_ns::socket_ptr(_tcpsvr->Accept(&addr));
                if(sp->get() == nullptr)
                {
                    delete sp;
                    continue;
                }
                pthread_t tid;
                pthread_create(&tid,nullptr,Entrance::HandlerRequest,sp);
                pthread_detach(tid);
            }
        }
    private:
        TcpServer* _tcpsvr;
        bool _running;
};


#endif